package com.fingard.dsp.bank.directbank.hrbb01;

import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class HRBB01SinglePay extends HRBB01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
        if ("1".equals(p_req1908.singleDetail.sameBnk)) {//同行
            tmpRetDetail = bankInnerTransfer(p_req1908);
        } else {//跨行
            tmpRetDetail = crossBankTransfer(p_req1908);
        }
        if ("-1".equals(tmpRetDetail.respCode) && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    //跨行转账
    private RetAtsSglDetail crossBankTransfer(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        HRBB01Header header = new HRBB01Header();
        header.bsnCode = "CBE004";
        header.serialNo = p_req1908.singleDetail.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, header);
        XElement tmpXElement = new XElement("ReqParam", "");
        tmpXElement.addChild(new XElement("payAccount", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("payAccountName", p_req1908.singleDetail.payName));
        tmpXElement.addChild(new XElement("recAccount", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("recAccountName", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("payAmount", p_req1908.singleDetail.payAmount));
        String tmpCur = convertCurToBank(getBankConfig().getMapKeyByValue("curActMapping", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("currencyType", tmpCur));
        tmpXElement.addChild(new XElement("payUse", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("unionBankNo", p_req1908.singleDetail.cnapsCode));
        float tmpPayAmt = Float.parseFloat(p_req1908.singleDetail.payAmount);
        String rmtType = tmpPayAmt > getBankConfig().getValueAsFloat("spdAmt") ? "1" : "2";
        tmpXElement.addChild(new XElement("rmtType", rmtType));

        nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());

        String tmpSignData = "交易码#" + header.bsnCode + "|请求时间#" + reqTime + "|付款账号#" + p_req1908.singleDetail.payAct + "|付款户名#" + p_req1908.singleDetail.payName + "|收款账号#"
                + p_req1908.singleDetail.recAct + "|收款户名#" + p_req1908.singleDetail.recName + "|交易金额#" + p_req1908.singleDetail.payAmount + "|联行号#" + p_req1908.singleDetail.cnapsCode;

        if (signature(tmpSignData)) {
            tmpXElement.addChild(new XElement("signData", signedData));
            tmpXElement.addChild(new XElement("signDataField", tmpSignData));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                if (getRet(tmpStrRet[1])) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRets[2], tmpSbMsg);
                    if (tmpBkReader != null) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.getFirstText("/BankData/opRep/opResult/responseResult");
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        //成功：90  失败：99  可疑：98
                        retDetail.transState = tmpBkReader.getFirstText("/BankData/opRep/opResult/orderState");
                        retDetail.payInfoCode = retDetail.transState;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpRets[1];
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = "签名失败";
        }
        return retDetail;
    }

    //同行转账
    private RetAtsSglDetail bankInnerTransfer(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        HRBB01Header header = new HRBB01Header();
        header.bsnCode = "CBE003";
        header.serialNo = p_req1908.singleDetail.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, header);
        XElement tmpXElement = new XElement("ReqParam", "");
        tmpXElement.addChild(new XElement("payAccount", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("payAccountName", p_req1908.singleDetail.payName));
        tmpXElement.addChild(new XElement("recAccount", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("recAccountName", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("payAmount", p_req1908.singleDetail.payAmount));
        String tmpCur = convertCurToBank(getBankConfig().getMapKeyByValue("curActMapping", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("currencyType", tmpCur));
        tmpXElement.addChild(new XElement("payUse", p_req1908.singleDetail.purpose));

        nodeAnalysic(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());

        String tmpSignData = "交易码#" + header.bsnCode + "|请求时间#" + reqTime + "|付款账号#" + p_req1908.singleDetail.payAct + "|收款账号#"
                + p_req1908.singleDetail.recAct + "|收款户名#" + p_req1908.singleDetail.recName + "|交易金额#" + p_req1908.singleDetail.payAmount;
        if (signature(tmpSignData)) {
            tmpXElement.addChild(new XElement("signData", signedData));
            tmpXElement.addChild(new XElement("signDataField", tmpSignData));

            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                if (getRet(tmpStrRet[1])) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRets[2], tmpSbMsg);
                    if (tmpBkReader != null) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.getFirstText("/BankData/opRep/opResult/responseResult");
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        //成功：90  失败：99  可疑：98
                        retDetail.transState = tmpBkReader.getFirstText("/BankData/opRep/opResult/orderState");
                        retDetail.payInfoCode = retDetail.transState;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    retDetail.respCode = "-1";
                    if (tmpRets.length >= 3) {
                        retDetail.respInfo = tmpRets[1];
                    } else {
                        retDetail.respInfo = tmpRets[0];
                    }
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = "签名失败";
        }
        return retDetail;
    }

    //状态查询
    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        HRBB01Header header = new HRBB01Header();
        header.bsnCode = "CBE010";
        header.serialNo = p_req1944.reqHeader.transSeqID;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, header);
        xmlWriter.writeStartElement("ReqParam");
        xmlWriter.writeElementString("orderFlowNo", p_req1944.askNumber);
        xmlWriter.writeElementString("queryBsnCode", "1".equals(p_req1944.sameBnk) ? "CBE003" : "CBE004");
        xmlWriter.writeElementString("reqReserved1", "");
        xmlWriter.writeElementString("reqReserved2", "");
        xmlWriter.writeEndDocument();
        try {
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                if (getRet(tmpStrRet[1])) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRets[2], tmpSbMsg);
                    if (tmpBkReader != null) {
                        retDetail.reqSeqID = p_req1944.askNumber;
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.getFirstText("/BankData/opRep/opResult/responseResult");
                        //交易状态与电子金融平台保持一致。
                        retDetail.transState = tmpBkReader.getFirstText("/BankData/opRep/opResult/orderState");
                        retDetail.payInfoCode = retDetail.transState;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpRets[1];
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
